﻿<html>
	<head>
		<title>Hirdetett termények</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link rel="stylesheet" href="v-a.css" type="text/css">
	</head>
	<?php	
		session_start();
		$_SESSION['sh']="";
		//print_r ($_POST);
		require_once ('dbConnect.php');
		include ('functions.php');

		if(isset($_POST['Vissza'])) {
			$_SESSION['uzenet']="";
			header('Location: index.php');
			exit;
		}
		if(isset($_POST['saját_hirdetések'])) {
			$_SESSION['sh']=$_SESSION['username'];
		}
		if(isset($_POST['lefoglal'])) {
			$_SESSION['uzenet']="";
			//$_SESSION['editedhirdetes']=$_POST['hirdetesID'][firstkey($_POST['lefoglal'])];
			//print_r($_POST['mennyiseg'][firstkey($_POST['lefoglal'])]);
			
			$result=MyDatabase::getConnection()->multi_query("CALL foglalas('".$_POST['hirdetesID'][firstkey($_POST['lefoglal'])]."','".$_SESSION['username']."','".$_POST['mennyiseg'][firstkey($_POST['lefoglal'])]."')");
			if (!$result) {
				$_SESSION['uzenet']='mysql_error: ' . MyDatabase::getConnection()->error;
			}
			else{
				$_SESSION['uzenet']='Lefoglalva';
				
			}
			//exit;
		}
		if(isset($_POST['új'])) {
			$_SESSION['uzenet']="";
			$_SESSION['editedhirdetes']="";
			$_SESSION['vissza']="hirdeteslist.php";
			header('Location: hirdetesedit.php');
			exit;
		}
		
		//if ((checkuser("admin")==0) and (checkuser("termelő")==0)){
			//$_SESSION['uzenet']= "access denied";
		//}
		
	?>
	
	<body>
		<table  ><tr HEIGHT="10"><td WIDTH="80%">Hirdetett termények</td><td WIDTH="20%"><?php include('kilepes.php');?></td></tr></table>
		
		<hr>
		
		<?php 
			printf($_SESSION['uzenet']);
		?>
		<hr>
		<form name='menu' method='post' action="hirdeteslist.php">
		<table>
		<tr>
		<td>
			<input type='submit' class='button' name='Vissza' value='<< vissza' />
		</td>
		<?php 
			if ((checkuser("admin")==1) or (checkuser("termelő")==1))
			echo "<td>
				<input type='submit' class='button' name='új' value='új' />
			</td>";
		?>
		</tr>
		</table>

			<?php 
			
			if ((checkuser("admin")==1) or (checkuser("termelő")==1) or (checkuser("vásárló")==1)){
				$query="CALL hirdetmenyek('".$_SESSION['sh']."');";
				if (MyDatabase::getConnection()->multi_query($query)) {
						echo "<table id='one-column-emphasis'>
						<td>ID</td><td>meghirdetés időpontja</td><td>mennyiség</td><td>mértékegység</td><td>megnevezés</td><td>ár</td><td>min.rendelés</td><td>hirdető</td><td>termékfotó</td><td>foglalási mennyiség</td><td></td>";
						
						if ($result = MyDatabase::getConnection()->store_result()) {
							$j=0;
							while ($row = $result->fetch_row()) {
								echo "<tr>";
								echo "<input type=hidden name=hirdetesID[$j] value=$row[0]><td>$row[0]</td>";
								echo "<td>$row[8]</td>";
								for ($i=1;$i<7;$i++){
									echo "<td>".$row[$i]."</td>";
								}
								echo '<td><img src="data:image/jpeg;base64,' . base64_encode( $row[7] ) . '" width="100" /></td>';
								echo "<td><input type=text size=2 name=mennyiseg[$j] ></td>
								<td><input type='submit' class='tablazatbutton' name='lefoglal[$j]' value='lefoglal' /></td>";
								$j++;
								echo "</tr>";
							}
							$result->free();
						}
						echo "</table>";
				}
				else{
					$_SESSION['uzenet']= MyDatabase::getConnection()->error;
					//printf(MyDatabase::getConnection()->error);
				}
			}
			?>
		</form>
	</body>
</html>

